﻿using System;
using System.Collections.Generic;
using System.Text;
using MFD.SharePoint.Notifications;
using System.Reflection;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
using System.Web.UI;
using System.IO;
using System.Web.UI.WebControls.WebParts;


namespace MFD.SharePoint.TaskNotification
{
    internal class TaskNotificationValues
    {
        public string TaskTitle { get; set; }
        public string TaskLink { get; set; }
        public int DaysToStartDate { get; set; }
        public int DaysFromStartDate { get; set; }
        public int DaysToDueDate { get; set; }
        public int DaysFromDueDate { get; set; }

        public string TaskHTML { get; set; }

        public TaskNotificationValues(SPListItem task)
        {
           
            TaskTitle = task.Title;
            TaskLink = string.Format("href=\"{0}/{1}?ID={2}\"",task.Web.Url, task.ParentList.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url, task.ID);
            DateTime startDate = task["StartDate"] == null ? DateTime.MaxValue : (DateTime)task["StartDate"];
            DateTime dueDate = task["DueDate"]==null? DateTime.MaxValue:(DateTime)task["DueDate"];

            DaysToStartDate = (startDate - DateTime.Today).Days;
            DaysFromStartDate = (DateTime.Today - startDate).Days;
            DaysToDueDate = (dueDate - DateTime.Today).Days;
            DaysFromDueDate = (DateTime.Today - dueDate).Days;

            //TODO: get the item HTML to send it by email
            //try to use ListFormWebPart 
            //SPQuery queryItem = new SPQuery();
            //queryItem.Query = string.Format("<Where><Eq><FieldRef Name='ID'/><Value Type='Integer'>{0}</Value></Eq></Where>",task.ID);
            //TaskHTML = task.ParentList.RenderAsHtml(queryItem);
            //TaskHTML = GetItemHTML(task);
        }


        private string GetItemHTML(SPListItem task)
        {
            StringBuilder ret = new StringBuilder() ;

            
            ret.AppendLine("<Table class=\"ms-formtable\" style=\"margin-top: 8px;\" border=0 cellpadding=0 cellspacing=0 width=100%>");
			
            
            foreach(string field in task.ParentList.DefaultView.ViewFields)
            {
                ret.AppendLine("<tr>");

                ret.AppendLine("<td nowrap=\"true\" valign=\"top\" width=\"165px\" class=\"ms-formlabel\">");
                    ret.AppendLine(field);
                ret.AppendLine("</td>");

                ret.AppendLine("<td valign=\"top\" class=\"ms-formbody\" width=\"450px\">");
                    ret.AppendLine(task.GetFormattedValue(field));
                ret.AppendLine("</td>");

                ret.AppendLine("</tr>");
            }
			
			
		
            
            ret.AppendLine("</Table>");
            return ret.ToString();

        }

        public string DaysToStartDateText
        {
            get
            {
                return DaysToStartDate == 0 ? "Today" : string.Format("in {0} days", DaysToStartDate);
            }
        }

        public string DaysFromStartDateText
        {
            get
            {
                return DaysFromStartDate == 0 ? "Today" : string.Format("{0} days ago", DaysFromStartDate);
            }
        }

        public string DaysToDueDateText
        {
            get
            {
                return DaysToDueDate == 0 ? "Today" : string.Format("in {0} days", DaysToDueDate);
            }
        }

        public string DaysFromDueDateText
        {
            get
            {
                return DaysFromDueDate == 0 ? "Today" : string.Format("{0} days ago", DaysFromDueDate);
            }
        }
    
    }

    
}
